package com.das.mechanic_base.adapter.common;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.bean.main.CarBrandBean;
import com.das.mechanic_base.bean.main.CarModelSelectBean;
import com.das.mechanic_base.utils.X3StringUtils;

import java.util.ArrayList;
import java.util.List;

public class X3SelectCarModelAdapter extends RecyclerView.Adapter<X3SelectCarModelAdapter.SelectCarModelHolder> {

    private List<CarBrandBean> mList;

    private Context mContext;
    private List<Boolean> sList;
    private String brandValue;
    private String brandName;
    private  List<CarModelSelectBean> selectList;
    private  List<Integer> posList;
    private List<Integer> addList ;

    public X3SelectCarModelAdapter(Context mContext, String brandValue, String brandName) {
        this.mContext = mContext;
        this.mList = new ArrayList<>();
        this.sList = new ArrayList<>();
        this.posList = new ArrayList<>();
        this.addList = new ArrayList<>();
        this.brandName = brandName;
        this.brandValue = brandValue;
    }

    public void changeData(List<CarBrandBean> mList, List<CarModelSelectBean> selectList) {
        this.mList = mList;
        this.sList.clear();
        this.posList.clear();
        this.selectList = selectList;//选中的结合
        for (int i = 0; i < mList.size(); i++) {   //  1  2  3     1 2 3 4    记录索引
            for (int j = 0; j < selectList.size(); j++){
                if(selectList.get(j).modelValue.equals(mList.get(i).getValue())){//相等时的索引
                    posList.add(i);
                }
                if(selectList.get(j).brandValue.equals(brandValue) && selectList.get(j).modelValue.equals("ALL")){
                    posList.add(i);
                }
            }
            sList.add(false);
        }
        for(int k =0; k < posList.size(); k++){
             sList.set(posList.get(k),true);
        }

        notifyDataSetChanged();
    }

    /**
     * @param
     */
    public void isSelelctAll(boolean isSelect){
        this.addList.clear();
        for (int i = 0; i < sList.size(); i++){
            if (sList.get(i) == isSelect) {
                continue;
            }
            if(isSelect){
                if(!sList.get(i)){
                    sList.remove(i);
                    sList.add(i,true);
                    addList.add(i);//记录从fasle变为true的   全选时 只返回从fale变为true 的数据
                }
            }else {
                sList.remove(i);
                sList.add(i,false);
            }

        }

        getSelectList(isSelect);

        notifyDataSetChanged();
    }

    public void getSelectList(boolean isSelect) {
        List<CarModelSelectBean> modelList = new ArrayList<>();
        if(isSelect){
            for (int i = 0; i < addList.size(); i++){
                if (sList.get(i) == !isSelect) {
                    continue;
                }
                CarModelSelectBean carModelSelectBean = new CarModelSelectBean();
                carModelSelectBean.brandName = brandName;
                carModelSelectBean.brandValue = brandValue;
                carModelSelectBean.modelName = mList.get(addList.get(i)).getLabel();
                carModelSelectBean.modelValue = mList.get(addList.get(i)).getValue();
                modelList.add(carModelSelectBean);
            }

        }else {
            for (int i = 0;i < mList.size(); i++) {
                if (sList.get(i) == !isSelect) {
                    continue;
                }
                CarModelSelectBean carModelSelectBean = new CarModelSelectBean();
                carModelSelectBean.brandName = brandName;
                carModelSelectBean.brandValue = brandValue;
                carModelSelectBean.modelName = mList.get(i).getLabel();
                carModelSelectBean.modelValue = mList.get(i).getValue();
                modelList.add(carModelSelectBean);
            }

        }

        if (iOnSelectStyle != null) {
            iOnSelectStyle.iOnCarModelIsSelect(modelList,isSelect,"all",brandName,brandValue);
        }
    }

    public void clearSelelctAll(){
        for (int i = 0; i < sList.size(); i++){
            if (!sList.get(i)) {
                continue;
            }
            sList.remove(i);
            sList.add(i,false);
        }

        getSelectList(false);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public X3SelectCarModelAdapter.SelectCarModelHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        return new X3SelectCarModelAdapter.SelectCarModelHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_select_car_model_item,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull X3SelectCarModelAdapter.SelectCarModelHolder holder, int position) {
        holder.tv_name.setText(mList.get(position).getLabel() + "");
        if (sList.get(position)) {
            holder.iv_select.setImageResource(R.mipmap.x3_edit_light_select);
        }else {
            holder.iv_select.setImageResource(R.mipmap.x3_edit_light_default);
        }
        holder.itemView.setOnClickListener(view -> {
            Boolean aBoolean = sList.get(position);
            sList.remove(position);
            sList.add(position,!aBoolean);
            CarModelSelectBean carModelSelectBean = new CarModelSelectBean();
            carModelSelectBean.brandName = brandName;
            carModelSelectBean.brandValue = brandValue;
            carModelSelectBean.modelName = mList.get(position).getLabel();
            carModelSelectBean.modelValue = mList.get(position).getValue();
            List<CarModelSelectBean> selectBeans = new ArrayList<>();
            selectBeans.add(carModelSelectBean);
            if (iOnSelectStyle != null) {
                iOnSelectStyle.iOnCarModelIsSelect(selectBeans,sList.get(position),"single",brandName,brandValue);
            }

            notifyItemChanged(position);
        });

    }
    public boolean isGroupHead(int position) {
        if (position >= mList.size()) {
            return false;
        }
        if (position == 0) {
            return true;
        } else {
            String currentGroup = getGroupName(position);
            String preGroup = getGroupName(position - 1);
            return !currentGroup.equals(preGroup);
        }
    }

    /**
     * 获取组名
     */
    public String getGroupName(int position){

        String sortLetters = mList.get(position).getSortLetters();

        return "🔥".equals(sortLetters) ? mContext.getString(R.string.x3_common_brand) : sortLetters;
    }

    IOnSelectStyle iOnSelectStyle;

    public void setiOnSelectStyle(IOnSelectStyle iOnSelectStyle) {
        this.iOnSelectStyle = iOnSelectStyle;
    }

    public interface IOnSelectStyle {
        /**
         * 是否选中或者取消选中
         * @param selectListAll
         * @param isAdd
         */
        void iOnCarModelIsSelect(List<CarModelSelectBean> selectListAll,boolean isAdd,String type,String brandName,String brandValue);
    }

    @Override
    public int getItemViewType(int position) {
        if(X3StringUtils.isListEmpty(mList)){
            return 0;
        }
        return 1;
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class SelectCarModelHolder extends RecyclerView.ViewHolder {
        TextView tv_name;
        ImageView iv_select;
        public SelectCarModelHolder(@NonNull View itemView) {
            super(itemView);
            tv_name = itemView.findViewById(R.id.tv_name);
            iv_select = itemView.findViewById(R.id.iv_select);
        }
    }
}
